function findStr(source, pattern) {
  for (let i = 0; i < source.length; i++) {
    for (let j = 0; j < pattern.length; j++) {
      if (source[i + j] !== pattern[j]) {
        break
      }
      if (j === pattern.length - 1) {
        return i
      }
    }
  }
  return -1
}


function findSubStr(source, pattern) {
  outer: for (let i = 0; i < source.length; i++) {
    inner: for (let j = 0; j < pattern.length; j++) {
      if (source[j + i] !== pattern[j]) {
        continue outer
      }
    }
    return i
  }
  return -1
}


function findStr2(source, pattern) {
  for (let i = 0; i < source.length; i++) {
    let result = true
    for (let j = 0; j < pattern.length; j++) {
      if (source[i + j] !== pattern[j]) {
        result = false
        break
      }
    }
    if (result) return i
  }
  return -1
}
//eabcdef
//abc
function findStr3(source, pattern) {
  let j = 0
  for (let i = 0; i < source.length; i++) {
    console.log({
      i: source[i]
    })
    if (source[i] === pattern[j]) {
      j++
    } else {
      j = 0
      if (source[i] === pattern[j]) {
        // j++
        i++
      }
    }

    // if (source[i] !== pattern[j]) {
    //   j = 0
    //   if (source[i] === pattern[j]) {
    //     i++
    //   }
    // } else {
    //   j++
    // }

    if (j == pattern.length) {
      console.log({ i })
      return i - pattern.length + 1
    }
  }
  return -1
}
module.exports = {
  findStr,
  findSubStr,
  findStr2,
  findStr3
}
